from django.db import models

from common.models import User, Area, House


class BaseModel(models.Model):
    create_time = models.DateTimeField(auto_now_add=True, null=True)
    update_time = models.DateTimeField(auto_now=True, null=True)

    class Meta:
        abstract = True


class Order(BaseModel, models.Model):
    begin_date = models.DateTimeField(null=False)  # 入住时间
    end_date = models.DateTimeField(null=False)  # 离店时间
    days = models.IntegerField(null=False)  # 住多少天
    house_price = models.IntegerField(null=False)  # 房间价格
    amount = models.IntegerField(null=False)  # 总价格
    status_enum = (
        ("WAIT_ACCEPT", "待接单"),  # 待接单
        ("WAIT_PAYMENT", "待支付"),  # 待支付
        ("PAID", "已支付"),   # 已支付
        ("WAIT_COMMENT", "待评价"),  # 待评价
        ("COMPLETE", "已完成"),  # 已完成
        ("CANCELED", "已取消"),  # 已取消
        ("REJECTED", "已拒单")  # 已拒单
    )
    status = models.CharField(choices=status_enum, default="WAIT_ACCEPT", verbose_name='STATUS', max_length=20)
    comment = models.TextField()  # 评论
    reject_reason = models.TextField(null=True)  # 拒单原因

    # 多对一关联用户
    user_order = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    # 多对一关联地区
    area_order = models.ForeignKey(Area, on_delete=models.CASCADE, null=True)
    # 多对一关联房屋
    house_order = models.ForeignKey(House, on_delete=models.CASCADE, null=True)

    class Meta:
        db_table = 'ihome_order'


